<?php
require_once 'Complain/ActionAddComplain.php';
require_once 'Complain/ActionResponseComplain.php';
require_once APPLICATION_PATH.'\misc\Paging.php';
require_once APPLICATION_PATH.'\misc\sessionController.php';
require_once APPLICATION_PATH.'\misc\exceptionHandler.php';
class ComplainController extends Zend_Controller_Action
{   private $controller='Complain';
    private $paramName=array('Super','Administrator');
    public function init()
    {  $this->view->message = $this->_helper->_flashMessenger->getMessages();
       $this->item_per_page = Paging::$number;
       $sessionController=new sessionController();
        if($sessionController->checkSessionAdmin($this->paramName)){
                $this->view->sessioncontroller = $sessionController;
         }else{
                  $this->_helper->redirector('login','admin');
         }
     }

    public function indexAction()
    {
        // action body
    }

    public function addAction()
    {
        //initialize validation class
        Zend_Session::start();
        $sessionController=new Zend_Session_Namespace('superadmin');
        $complainController = new Application_Controllers_Complain_ActionAddComplain();
        $form = new Application_Form_Complain();
        $form->title->setLabel('Judul');
        $form->complain->setLabel('Komplen');
        $form->submit->setLabel('kirim');
        $form->reset->setLabel('Reset');
        $form->buyer_id->setValue($sessionController->id);
        $formDisplay=new Application_Form_Display();
        $formDisplay->addElements(array($form->buyer_id,$form->title,$form->complain,
                                 $form->submit,$form->reset));
        
        $this->view->form = $formDisplay;
        $this->view->errorMessage=$sessionController->id;
        if($this->getRequest()->isPost()){
            $postData = $this->getRequest()->getPost();
            if($formDisplay->isValid($postData)){
                if($complainController->preAdd($formDisplay)){
                    $complainController->onAdd($formDisplay);
                }else{
                    $this->view->errorMessage=$complainController->errorMessage;
                    $formDisplay->populate($postData);
                }
            }else{
               $formDisplay->populate($postData);
            }
        }
    }
    public function removeAction()
    {
        // action body
    }

    public function listAction()
    {   
        try{   
                $paging = new Paging();
                $complain = new Application_Model_DbTable_Complain();
                $this->view->buyer= new Application_Model_DbTable_Buyers();
                $page=  $this->_getParam('page', 1);
                $order=  $this->_getParam('order', "id");
                $method = $this->_getParam('method', "asc");
                $name = strtolower($this->_getparam('name',''));

                $where="";
                if($name!=""){
                    $where = " c.name like '%".$name."%'";
                }
                $this->view->complain = $complain->getAll($where,$order,$method,$page, Paging::$number);
                $this->view->page=$page;
                $this->view->order=$order;
                $this->view->premethod=$method;
                if($method=='asc'){
                    $this->view->method='desc';
                }else{
                    $this->view->method='asc';
                }

                $this->view->name=$name;
                $this->view->totalPages=  Paging::totalPages($complain->countRows($where));
                $this->view->back=  $paging->back($this->controller, 'list', $page, $order, $method,$name);
                $this->view->next=  $paging->next($this->controller, 'list',
                            $this->view->totalPages, $page, $order, $method,$name);
                $this->view->current = $paging->current($this->controller, 'list',
                        $this->view->totalPages, $page, $order, $method,$name);
        }catch(Zend_Exception $e){
           $exceptionHandler = new exceptionHandler();
           $exceptionHandler->addLog($e, $this->classname);}

    }

    public function detailsAction()
    {
        try{

            $sessionController=new sessionController();
            if($sessionController->checkSessionAdmin($this->paramName)){
                $id = $this->_getParam('id', 0);
                $complain = new Application_Model_DbTable_Complain();
                $row=$complain->fetchRow('id='.(int) $id);
                $this->view->buyer= new Application_Model_DbTable_Buyers();
                $admin = new Application_Model_DbTable_Admin();
                if($row['status']==1){
                    $this->view->admin = $admin->getAdmin($row['admin_id']);
                }
                $this->view->complain = $row;}else{
                $this->_helper->redirector('login','admin');}
        }catch(Zend_Exception $e){
           $exceptionHandler = new exceptionHandler();
           $exceptionHandler->addLog($e, $this->classname);}
    }

    public function responseAction()
    {
     

        $sessionController=new sessionController();
        if($sessionController->checkSessionAdmin($this->paramName)){
        $id = $this->_getParam('id', 0);
        $complain = new Application_Model_DbTable_Complain();
        $this->view->buyer= new Application_Model_DbTable_Buyers();
        $admin = new Application_Model_DbTable_Admin();
        $complainController = new Application_Controllers_Complain_ActionResponseComplain();
        $form = new Application_Form_Complain();
        $form->id->setValue($id);
        $form->response_msg->setLabel('Jawaban');
        $form->submit->setLabel('Jawab');
        $form->reset->setLabel('Reset');
        $form->admin_id->setValue($sessionController->getSessionId());
        $formDisplay=new Application_Form_Display();
        $formDisplay->addElements(array($form->id,$form->admin_id,$form->response_msg,
                                 $form->submit,$form->reset));

        $row=$complain->fetchRow('id='.(int) $id);
        $this->view->form = $formDisplay;
        $this->view->complain = $row;
        if($this->getRequest()->isPost()){
            $postData = $this->getRequest()->getPost();
            if($formDisplay->isValid($postData)){
                if($complainController->preResponse($formDisplay)){
                    $complainController->onResponse($formDisplay);
                    $this->_helper->_flashMessenger->addMessage("Keluhan telah berhasil dijawab");

//                    $this->view->errorMessage="sukses";
                    $this->_helper->redirector('list');
                }else{
                    $this->view->errorMessage=$complainController->errorMessage;
                    $formDisplay->populate($postData);
                }
            }else{
               $formDisplay->populate($postData);
            }
         }

      }else{
            $this->_helper->redirector('login','admin');}


}

}









